package com.zzj.portalwebsite.Controller;

import com.alibaba.fastjson.JSON;
import com.zzj.portalwebsite.Dao.Impl.ArticalInfoDaoImpl;
import com.zzj.portalwebsite.entity.article;
import com.zzj.portalwebsite.utlis.CommonResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/news/addNews")
public class AddNewsController extends HttpServlet {
    ArticalInfoDaoImpl articalInfoDao = new ArticalInfoDaoImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        try {
            String title = request.getParameter("title");
            String author = request.getParameter("author");
            String content = request.getParameter("content");
            String image = request.getParameter("image");
            System.out.println(title);
            System.out.println(author);
            article article = new article(null, title, content, author, null, 1, image);

            boolean b = articalInfoDao.addNews(article);
            if (b){
                response.getWriter().write(JSON.toJSONString(CommonResult.success(article)));
            }else{
                response.getWriter().write(JSON.toJSONString(CommonResult.failed("添加失败")));
            }


        }catch (Exception e){

            e.printStackTrace();
        }

    }
}
